package com.project.service.Imp;

import com.project.bean.ClassBean;
import com.project.mapper.IClassMapper;
import com.project.service.IClassService;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class ClassService extends BaseService implements IClassService {
    @Override
    public List<ClassBean> findAllClass() {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        List<ClassBean> allClass = mapper.findAllClass();
        return allClass;
    }

    @Override
    public boolean addClass(ClassBean classBean) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        ClassBean byClassName = mapper.findByClassName(classBean.getName());
        System.out.println(byClassName+"+++++++++");
        if (byClassName==null){
            mapper.addClass(classBean);
            session.commit();
            session.close();
            return true;
        }else {
            session.close();
            return false;
        }

    }

    @Override
    public boolean deleteClass(Integer classId) {
        SqlSession session = this.getSession();
        IClassMapper mapper = session.getMapper(IClassMapper.class);
        Integer byClassID = mapper.findByClassID(classId);
        System.out.println(byClassID+"++++++++++");
        if (byClassID==null){
            mapper.deleteClass(classId);
            session.commit();
            session.close();
            return true;
        }else {
            session.close();
            return false;
        }

    }
}
